<template>
  <div>
    <div :id="id" class="chart-container" :class="className" />
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancelDialog">
        Cancel
      </el-button>
    </div>
  </div>
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'

export default {
  name: 'ChartPic',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '1500px'
    },
    height: {
      type: String,
      default: '800px'
    }
  },
  data() {
    return {
      chart: null,
      dynamicData: {
        xAxisData: ['13:00', '13:05', '13:10', '13:15', '13:20', '13:25', '13:30', '13:35', '13:40', '13:45', '13:50', '13:55'],
        seriesData: [
          {
            name: 'CMCC',
            data: [220, 182, 191, 134, 150, 120, 110, 125, 145, 122, 165, 122]
          },
          {
            name: 'CTCC',
            data: [120, 110, 125, 145, 122, 165, 122, 220, 182, 191, 134, 150]
          },
          {
            name: 'CUCC',
            data: [220, 182, 125, 145, 122, 191, 134, 150, 120, 110, 165, 122]
          }
        ]
      }
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose()
      this.chart = null
    }
  },
  methods: {
    cancelDialog() {
      this.$emit('cancleDialogChart')
    },
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id))
    },
    updateChart(data) {
      const colors = ['#FF5722', '#009688', '#FFC107', '#607D8B', '#2196F3', '#9C27B0']
      const legendData = data.seriesData.map(series => series.name)
      const yAxis = data.seriesData.map((series, index) => ({
        type: 'value',
        name: series.name,
        position: index % 2 === 0 ? 'top' : 'bottom',
        axisLine: {
          lineStyle: {
            color: colors[index % colors.length]
          }
        },
        axisLabel: {
          formatter: `{value}`,
          textStyle: {
            color: colors[index % colors.length]
          }
        },
        splitLine: {
          show: index === 0, // 只在第一个Y轴上显示分隔线
          lineStyle: {
            color: '#57617B'
          }
        }
      }))

      this.chart.setOption({
        backgroundColor: '#394056',
        title: {
          top: 20,
          text: '',
          textStyle: {
            fontWeight: 'normal',
            fontSize: 16,
            color: '#F1F1F3'
          },
          left: '1%'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            lineStyle: {
              color: '#57617B'
            }
          }
        },
        legend: {
          top: 20,
          icon: 'rect',
          itemWidth: 14,
          itemHeight: 5,
          itemGap: 13,
          data: legendData,
          right: '4%',
          textStyle: {
            fontSize: 12,
            color: '#F1F1F3'
          }
        },
        grid: {
          top: 100,
          left: '2%',
          right: '2%',
          bottom: '2%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          boundaryGap: false,
          axisLine: {
            lineStyle: {
              color: '#57617B'
            }
          },
          data: data.xAxisData
        }],
        yAxis: yAxis,
        series: data.seriesData.map((series, index) => ({
          ...series,
          type: 'line',
          smooth: true,
          symbol: 'circle',
          symbolSize: 5,
          showSymbol: false,
          lineStyle: {
            normal: {
              width: 1,
              color: colors[index % colors.length] // 使用颜色数组中的颜色
            }
          },
          yAxisIndex: index, // 指定使用的Y轴索引
          areaStyle: {
            normal: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: colors[index % colors.length] // 开始颜色
                }, {
                  offset: 1,
                  color: 'rgba(255,255,255,0)' // 结束颜色透明
                }
              ])
            }
          },
          itemStyle: {
            normal: {
              color: colors[index % colors.length], // 使用颜色数组中的颜色
              borderColor: 'rgba(137,189,27,0.27)', // 使用固定的边框颜色，或者你可以自定义
              borderWidth: 12
            }
          }
        }))
      }, true)
    },
    importData(data) {
      console.log('chartsdata', data)
      this.updateChart(data)
    }
  }
}
</script>

<style scoped>
.chart-container {
  width: 100%;
  height: 700px; /* 或者其他固定高度 */
}
</style>
